home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / PPP-Prefs / PPP-Prefs.c next >
Encoding:
C/C++ Source or Header  |  1995-12-06  |  5.7 KB  |  191 lines  |  [TEXT/CWIE]

  1. /********************************************************************************************
  2.  * PPP-Prefs.c                                                                                *
  3.  *                                                                                            *
  4.  *        These routines were designed to handle the preferences for MacPPP.                    *
  5.  *        Written by Tony Andreoli, 12/5/95                                                    *
  6.  *                                                                                            *
  7.  *        You may use these routines freely, I only ask that you credit me somewhere in your    *
  8.  *        code.                                                                                *
  9.  *                                                                                            *
  10.  *    Notes:                                                                                    *
  11.  *        • HandleError is just a simple error handler.                                        *
  12.  *        • OpenPPPPrefs simply opens the PPP Preferences file, and returns the fRef.            *
  13.  *        • MacPPP considers the PREF the first part of the data fork, which consists of        *
  14.  *          settings that do not change across sets.  It considers a CONFIG those settings    *
  15.  *          specific to a set.                                                                *
  16.  *        • The config index starts at 0, yet thePref.max_config starts at 1.  Thus if         *
  17.  *          thePref.max_config=4, the last config is 3 (0..3).                                *
  18.  *        • Be sure to look in the PPP-Prefs.h file for the structure of the prefs and        *
  19.  *          config.                                                                            *
  20.  *        • Each routine opens then closes the PPP Preferences file.  It is NOT left open        *
  21.  *          upon return.                                                                        *
  22.  *                                                                                            *
  23.  *    Purpose:                                                                                *
  24.  *        GetPPPPref - Gets the PPP preferences.                                                *
  25.  *        GetPPPConfig - Gets a specified config set.                                            *
  26.  *        PutPPPPref - Writes PPP preferences.                                                *
  27.  *        PutPPPConfig - Writes config to a specified set.                                    *
  28.  *        DeletePPPConfig - Deletes a specified (indexed) config, decrements max_config.        *
  29.  *        AddPPPConfig - Adds config to end of data fork, increments max_config.                *
  30.  *                                                                                            *
  31.  *    The following code is pretty worthless, but it does show some examples of useage.        *
  32.  *                                                                                            *
  33.  *    Example:                                                                                *
  34.  *        struct    ppp_pref thePref;                                                            *
  35.  *        struct    ppp_config theConfig;                                                        *
  36.  *                                                                                            *
  37.  *        GetPPPPref(&thePref);                                                                *
  38.  *        GetPPPConfig(&theConfig,thePref.active_config);                                        *
  39.  *        ParamText(thePref.portname,theConfig.config_name,theConfig.commands[1].scriptstr,"\p"); *
  40.  *        Alert(128,nil);                                                                        *
  41.  *                                                                                            *
  42.  *        strcpy((char *)thePref.portname,(char *)"\pPrinter-Modem Port");                    *
  43.  *        strcpy((char *)theConfig.config_name,(char *)"\pDidIMakeIt2?");                        *
  44.  *        PutPPPPref(thePref);                                                                *
  45.  *        PutPPPConfig(theConfig,1);                                                            *
  46.  *                                                                                            *
  47.  *        DeletePPPConfig(2);                                                                    *
  48.  *        AddPPPConfig(theConfig);                                                            *
  49.  ********************************************************************************************/
  50.  
  51. #include "PPP-Prefs.h"
  52.  
  53. #define thePrefsFile "\pPPP Preferences"
  54.  
  55. void HandleError(int errNum, Str255 errStr)
  56. {
  57.     Str255    theErr;
  58.     
  59.     if (errNum!=noErr) {
  60.         NumToString(errNum,theErr);
  61.         ParamText(theErr,errStr,"\p","\p");
  62.         Alert(128,nil);
  63.         ExitToShell();
  64.     }
  65. }
  66.  
  67. short OpenPPPPrefs(void)
  68. {
  69.     OSErr    err;
  70.     short    foundvRefNum;
  71.     long    foundDirID;
  72.     short    SystemFolderID;
  73.     short    theResFile;
  74.     FInfo    thefInfo;
  75.     
  76.     err=FindFolder(kOnSystemDisk,kPreferencesFolderType,kDontCreateFolder,&foundvRefNum,&foundDirID);    /* Get location of the preferences folder    */
  77.     err = HOpen(foundvRefNum,foundDirID,thePrefsFile,fsRdWrShPerm,&theResFile);
  78.     if (err != noErr) {
  79.         HandleError(err,"\pOpenPrefsFile:OpenResFile");
  80.     }
  81.     return theResFile;
  82. }
  83.  
  84. void GetPPPPref(struct ppp_pref *thePref)
  85. {
  86.     long    count;
  87.     OSErr    rc;
  88.     short    prefref;
  89.     Str255    theStr;
  90.  
  91.     prefref=OpenPPPPrefs();
  92.     rc = SetFPos(prefref,fsFromStart,0);
  93.     count = sizeof ( struct ppp_pref );
  94.     if ( rc == noErr) {
  95.         rc = FSRead(prefref, &count, thePref);
  96.     }
  97.     else
  98.         HandleError(rc,"\pSetFPos for PPP Prefs");
  99.     FSClose(prefref);
  100. }
  101.  
  102. void GetPPPConfig(struct ppp_config *theConfig,short confignum)
  103. {
  104.     long    count;
  105.     OSErr    rc;
  106.     short    prefref;
  107.  
  108.     prefref=OpenPPPPrefs();
  109.     rc = SetFPos(prefref,fsFromStart,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * confignum));
  110.     count = sizeof ( struct ppp_config );
  111.     if ( rc == noErr) {
  112.         rc = FSRead(prefref, &count, theConfig);
  113.     }
  114.     else
  115.         HandleError(rc,"\pSetFPos for PPP Config");
  116.     FSClose(prefref);
  117.     
  118. }
  119.  
  120. void PutPPPPref(struct ppp_pref thePref)
  121. {
  122.     long    count;
  123.     OSErr    rc;
  124.     short    prefref;
  125.     Str255    theStr;
  126.  
  127.     prefref=OpenPPPPrefs();
  128.     rc = SetFPos(prefref,fsFromStart,0);
  129.     count = sizeof ( struct ppp_pref );
  130.     if ( rc == noErr) {
  131.         rc = FSWrite(prefref, &count, &thePref);
  132.     }
  133.     else
  134.         HandleError(rc,"\pSetFPos for PPP Prefs");
  135.     FSClose(prefref);
  136. }
  137.  
  138. void PutPPPConfig(struct ppp_config theConfig,short confignum)
  139. {
  140.     long    count;
  141.     OSErr    rc;
  142.     short    prefref;
  143.  
  144.     prefref=OpenPPPPrefs();
  145.     rc = SetFPos(prefref,fsFromStart,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * confignum));
  146.     count = sizeof ( struct ppp_config );
  147.     if ( rc == noErr) {
  148.         rc = FSWrite(prefref, &count, &theConfig);
  149.     }
  150.     else
  151.         HandleError(rc,"\pSetFPos for PPP Config");
  152.     FSClose(prefref);
  153.     
  154. }
  155.  
  156. void DeletePPPConfig(short confignum)
  157. {
  158.     struct    ppp_pref thePref;
  159.     struct    ppp_config theConfig;
  160.     short    loop;
  161.     OSErr    err;
  162.     short    prefref;
  163.     
  164.     GetPPPPref(&thePref);
  165.     if (confignum <= (thePref.max_config-1)) {
  166.         for (loop=confignum;loop<=(thePref.max_config-1);loop++) {
  167.             GetPPPConfig(&theConfig,loop+1);
  168.             PutPPPConfig(theConfig,loop);
  169.         }
  170.         thePref.max_config--;
  171.         if (thePref.active_config>(thePref.max_config-1))
  172.             thePref.active_config=thePref.max_config-1;
  173.         PutPPPPref(thePref);
  174.         prefref=OpenPPPPrefs();
  175.         err = SetEOF(prefref,sizeof (struct ppp_pref)+(sizeof (struct ppp_config) * thePref.max_config));
  176.         FSClose(prefref);
  177.     }
  178. }
  179.  
  180. void AddPPPConfig(struct ppp_config theConfig)
  181. {
  182.     struct    ppp_pref thePref;
  183.     short    loop;
  184.     OSErr    err;
  185.     short    prefref;
  186.     
  187.     GetPPPPref(&thePref);
  188.     thePref.max_config++;
  189.     PutPPPConfig(theConfig,thePref.max_config-1);
  190.     PutPPPPref(thePref);
  191. }